package org.example.casLoginDemo.contast;

import com.alibaba.fastjson.JSONObject;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Serializable;

/**
 * 返回体
 * @author 何荣
 *
 */
public class Result implements Serializable {

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    //状态码
    private Integer code;

    //响应消息
    private String msg;

    //响应数据
    private Object data;

    public Result() {

    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }
    public Result ok() {
        this.code=1;
        return this;
    }

    public Result ok(Object data) {
        this.code=1;
        this.data=data;
        return this;
    }

    public Result fail(String msg) {
        this.code=0;
        this.msg = msg;
        return this;
    }

    public Result fail() {
        this.code=0;
        return this;
    }

    public Result(Integer code,String msg,Object data) {
        this.code=code;
        this.msg=msg;
        this.data=data;
    }

    //返回错误信息
    public static void setReturn(HttpServletResponse response, int status, String msg) throws IOException {
        HttpServletResponse httpResponse = (HttpServletResponse) response;

        httpResponse.setHeader("Access-Control-Allow-Credentials", "true");
        httpResponse.setCharacterEncoding("UTF-8");

        response.setContentType("application/json;charset=utf-8");
        Result res = new Result();
        res.fail(msg);
        httpResponse.getWriter().print(JSONObject.toJSONString(res));

    }

}